package com.zoweunion.mechanic.util.task;

import com.zoweunion.mechanic.model.things.ThingsData;
import com.zoweunion.mechanic.service.base.BaseService;
import com.zoweunion.mechanic.dao.app.ToolCarDataMapper;
import com.zoweunion.mechanic.service.app.ToolCarDataService;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.DateUtil;
import com.zoweunion.mechanic.util.cache.ThingsCacheUtil;
import com.zoweunion.mechanic.util.common.CarDataStatisticsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.time.LocalDate;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * 故障码数据导入到故障码历史数据中
 */
@Component
public class CarFaultCodeTask extends BaseService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ToolCarDataMapper toolCarDataMapper;
    @Autowired
    private ToolCarDataService toolCarDataService;
    @Autowired
    private ThingsCacheUtil thingsCacheUtil;

    @Resource(name = "cachedThreadPool")
    private ExecutorService cachedThreadPool;

    // @Scheduled(cron = "0 10 0 * * ? ")
    public void task(){
        // 获取所有绑定了故障模版的智能模版
        List<Map<String,Object>> intelligentTemplateList = toolCarDataMapper.getAllBindFaultCodeIntelligentTemplate();

        List<Map<String, Object>> dataList = new LinkedList<>();
        for (Map<String, Object> intelligentTemplate : intelligentTemplateList) {

            String intelligentTemplateId = intelligentTemplate.get("id").toString();
            String tableName = ThingsData.getTableName(intelligentTemplateId);
            String faultId = intelligentTemplate.get("faultId").toString();

            List<Map<String, Object>> carDataList = toolCarDataMapper.listYesterdayCarData(tableName, DateUtil.formatDate(LocalDate.now().minusDays(1)));
            if (carDataList.size() == 0) {
                continue;
            }

            List<Map<String, Object>> faultCodeList = thingsCacheUtil.getFaultDetailsByFaultTemplateId(faultId);
            for (Map<String, Object> carData : carDataList) {
                // 判断最新数据是否是为正常数据
                if (CarDataStatisticsUtil.equalsZero(carData.get(Constants.THINGS_FAULT_CODE_SPN + "1"))
                        && CarDataStatisticsUtil.equalsZero(carData.get(Constants.THINGS_FAULT_CODE_SPN + "2"))
                        && CarDataStatisticsUtil.equalsZero(carData.get(Constants.THINGS_FAULT_CODE_SPN + "3"))) {
                    continue;
                }

                // 封装故障码数据到dataList中
                toolCarDataService.addFaultCode(carData, faultCodeList, dataList, "1", intelligentTemplateId, faultId);
                toolCarDataService.addFaultCode(carData, faultCodeList, dataList, "2", intelligentTemplateId, faultId);
                toolCarDataService.addFaultCode(carData, faultCodeList, dataList, "3", intelligentTemplateId, faultId);
            }
        }
        if (dataList.size() == 0) {
            return;
        }
        int threadPageSize = 300;
        int threadCount = dataList.size() / threadPageSize + 1;
        Semaphore semaphore = new Semaphore(20);
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);

        for (int i = 0; i < threadCount; i++) {
            List<Map<String, Object>> subList;
            if (i == threadCount - 1) {
                subList = dataList.subList(i, dataList.size());
            } else {
                subList = dataList.subList(i, (i + 1) * threadPageSize);
            }
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("故障码数据导入到故障码历史数据定时任务结束");

    }
}
